﻿using CampWeek5__Book_Project_.Adapters.Interfaces;
using CampWeek5__Book_Project_.Data;
using CampWeek5__Book_Project_.Data.Model;
using CampWeek5__Book_Project_.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CampWeek5__Book_Project_.Adapters.Adapters
{
    public class AuthorAdapter : IAuthorAdapter
    {
        public AuthorVM CreateAuthor(AuthorVM author)
        {
            List<string> tags = author.Tags[0].Split(',').ToList();

            Author model = new Author()
            {
                AuthorName = author.AuthorName,
                DateOfBirth = author.DateOfBirth,
                DateOfDeath = author.DateOfDeath,
                AuthorCountry = author.AuthorCountry,
                AuthorImg = author.AuthorImg,
                AuthorBio = author.AuthorBio,
                TimesViewed = 0,
            };
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                db.Authors.Add(model);
                db.SaveChanges();

                foreach (var t in tags)
                {
                    string trimmedT = t.Trim();
                    if (db.Tags.Any(x => x.Description.ToLower() == trimmedT.ToLower()))
                    {
                        Tag tag = db.Tags.FirstOrDefault(x => x.Description.ToLower() == trimmedT.ToLower());
                        db.Author_Tags.Add(new Author_Tags() { TagId = tag.Id, AuthorId = model.Id });
                        db.SaveChanges();
                    }
                    else
                    {
                        Tag tag = new Tag() { Description = trimmedT };
                        db.Tags.Add(tag);
                        db.Author_Tags.Add(new Author_Tags() { AuthorId = model.Id, TagId = tag.Id });
                        db.SaveChanges();
                    }
                }
            }


            return (author);
        }

        public void AdminDeleteAuthor(int Id)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Author model = db.Authors.FirstOrDefault(x => x.Id == Id);
                model.DateDeleted = DateTime.Now;
                db.SaveChanges();
            }


        }

        public AuthorVM AdminGetAuthor(int Id)
        {
            AuthorVM model;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                model = db.Authors.Where(x => x.Id == Id).Select(x => new AuthorVM()
                {
                    AuthorName = x.AuthorName,
                    DateOfBirth = x.DateOfBirth,
                    DateOfDeath = x.DateOfDeath,
                    AuthorCountry = x.AuthorCountry,
                    AuthorImg = x.AuthorImg,
                    AuthorBio = x.AuthorBio,
                    TimesViewed = x.TimesViewed,
                    DateDeleted = x.DateDeleted,
                    Tags = x.Author_Tags.Select(t => t.Tag.Description).ToList(),
                    Id = x.Id,
                }
                ).FirstOrDefault();
            }
            return (model);
        }


        public AuthorVM AdminEditAuthor(AuthorVM model)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Author tempAuthor = db.Authors.FirstOrDefault(x => x.Id == model.Id);
                tempAuthor.Id = model.Id;  //is this needed?
                tempAuthor.AuthorName = model.AuthorName;
                tempAuthor.DateOfBirth = model.DateOfBirth;
                tempAuthor.DateOfDeath = model.DateOfDeath;
                tempAuthor.AuthorCountry = model.AuthorCountry;
                tempAuthor.AuthorImg = model.AuthorImg;
                tempAuthor.AuthorBio = model.AuthorBio;

               if (model.Tags.Count != 0)
                    {
                        List<string> NewTags = model.Tags[0].Split(',').ToList();
                        foreach (var t in NewTags)
                        {
                            string trimmedTag = t.Trim();
                            if (db.Tags.Any(x => x.Description.ToLower() == trimmedTag.ToLower()))
                            {
                                Tag tag = db.Tags.FirstOrDefault(x => x.Description.ToLower() == trimmedTag.ToLower());
                                db.Author_Tags.Add(new Author_Tags() { TagId = tag.Id, AuthorId = model.Id });
                                db.SaveChanges();
                            }
                            else
                            {
                                Tag tag = new Tag() { Description = trimmedTag };
                                db.Tags.Add(tag);
                                db.Author_Tags.Add(new Author_Tags() { AuthorId = model.Id, TagId = tag.Id });
                                db.SaveChanges();
                            }
                        }
                    }
            }
            return(model);
        } 
    }
}